﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Admin
{
    public partial class newsadd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            BindTopFlag();
            BindNewsCatogory();
        }
        /// <summary>
        /// 绑定TopFlag 从0到100; 
        /// 0为不置顶,其余从100-1倒序
        /// </summary>
        private void BindTopFlag()
        {   
            List<int> nddlTop = new List<int>();
            for (int i = 0; i <= 100; i++)
                nddlTop.Add(i);
            ddlTop.DataSource = nddlTop;
            ddlTop.DataBind();
        }

        private void BindNewsCatogory()
        {
            List<NewsCategoryInfo> list = new List<NewsCategoryInfo>();
            list = NewsCategoryInfoLogic.GetAllNewsCategoryInfo();
            if(list == null){
                list.Add(new NewsCategoryInfo());
            }
            ddlCategory.DataSource = list;
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            NewsInfo news = new NewsInfo();
            
            news.Title = txbTitle.Text.ToString();
            news.HotFlag = ckbHot.Checked ? 1 : 0;
            try{
                news.TopFlag = Int32.Parse(ddlTop.SelectedValue);
                news.CategoryID = Int32.Parse(ddlCategory.SelectedValue);
            }catch{
                news.TopFlag = 0;
                //此处发生异常应该立即停止....
                news.CategoryID = 1;
            }
            news.NewsPicURL = FileImgUpload();
            news.NewsContent = newsContent.Text.ToString();
            news.PublishTime = DateTime.Now;
            
            NewsInfoLogic.AddNewsInfo(news);
        }

        private string FileImgUpload()
        {
            string uploadName = fileImg.FileName;//获取待上传图片的完整路径，包括文件名 
            //string uploadName = InputFile.PostedFile.FileName; 
            string pictureName = "";//上传后的图片名，以当前时间为文件名，确保文件名没有重复 
            if (fileImg.FileName != "")
            {
                int idx = uploadName.LastIndexOf(".");
                string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 
                pictureName = DateTime.Now.Ticks.ToString() + suffix;
            }
            try
            {
                if (uploadName != "")
                {
                    string path = Server.MapPath("~/uploads/");
                    fileImg.PostedFile.SaveAs(path + pictureName);
                    return "/uploads/" + pictureName;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(ex);
            }
            return "";
        }
        NewsInfo createNewsInfo()
        {
            NewsInfo news = new NewsInfo();

            news.Title = txbTitle.Text.ToString();
            news.HotFlag = ckbHot.Checked ? 1 : 0;
            try
            {
                news.TopFlag = Int32.Parse(ddlTop.SelectedValue);
                news.CategoryID = Int32.Parse(ddlCategory.SelectedValue);
            }
            catch
            {
                news.TopFlag = 0;
                //此处发生异常应该立即停止....
                news.CategoryID = 1;
            }
            news.NewsContent = newsContent.Text.ToString();
            news.PublishTime = DateTime.Now;
            return news;
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            NewsInfo news = createNewsInfo();

            string ImgUrl = FileImgUpload();
            if(ImgUrl == ""){
                return;
            }
            news.NewsPicURL = ImgUrl;
        }
    }
}
